package timer
import (
	"time"
	"dao"
	"domain"
	"log"
	"strconv"
	"github.com/robfig/cron"
	"redisService"
)
func task (dba *dao.YcDB,runningorder *domain.RunningOrder) {
	log.Println(time.Unix(time.Now().Unix(), 0).String() + " 开始查询数据库" )
	startTime:=time.Now()
	result,err :=dba.QueryAvgTime()
	if err != nil {
		log.Println(time.Unix(time.Now().Unix(), 0).String() + " 数据库查询错误" + err.Error())
		runningorder.Setvalue(false,err,make([]map[string]interface{},0))
	}else {
		for index :=range result{
			var avgtime,meal_id,restaurantId string
			for key,val :=range result[index]{
				switch key {
				case "avgtime":
					avgtime=val.(string)
					if len(avgtime)==0 {
						avgtime="0"
					}
					break
				case "meal_id":
					meal_id=val.(string)
					if len(meal_id)==0 {
						meal_id=""
					}
					break
				case "restaurantId":
					restaurantId=val.(string)
					break
				}
			}
			_=avgtime;_=meal_id;_=restaurantId
			//				log.Println(avgtime+" "+meal_id+" "+restaurantId)
			redisService.AddResAvgtime(avgtime,restaurantId)

		}
		endTime:=time.Now()
		log.Println("统计平均时间，用时："+endTime.Sub(startTime).String())
		log.Println(time.Unix(time.Now().Unix(), 0).String() + " 数据库查询结果条数:" +strconv.Itoa(len(result)))
		//			resultformatjson,errr := json.Marshal(result);_=errr
		//			log.Println(time.Unix(time.Now().Unix(), 0).String() + " 数据库查询结果:" + string(resultformatjson))
		runningorder.Setvalue(true,err,result)
	}
}

// 统计平均时间
func QueryAvgTime(dba *dao.YcDB,runningorder *domain.RunningOrder)  {

	task(dba,runningorder)
	cron:=cron.New()
	cron.AddFunc("0 30 6 * * ?", func() {
	//cron.AddFunc("*/5 * * * * ?", func() {
		log.Println(time.Unix(time.Now().Unix(), 0).String() + " 开始查询数据库" )
		startTime:=time.Now()
		result,err :=dba.QueryAvgTime()
		if err != nil {
			log.Println(time.Unix(time.Now().Unix(), 0).String() + " 数据库查询错误" + err.Error())
			runningorder.Setvalue(false,err,make([]map[string]interface{},0))
		}else {
			for index :=range result{
				var avgtime,meal_id,restaurantId string
				for key,val :=range result[index]{
					switch key {
					case "avgtime":
						avgtime=val.(string)
						if len(avgtime)==0 {
							avgtime="0"
						}
						break
					case "meal_id":
						meal_id=val.(string)
						if len(meal_id)==0 {
							meal_id=""
						}
						break
					case "restaurantId":
						restaurantId=val.(string)
						break
					}
				}
				_=avgtime;_=meal_id;_=restaurantId
				//				log.Println(avgtime+" "+meal_id+" "+restaurantId)
				redisService.AddResAvgtime(avgtime,restaurantId)

			}
			endTime:=time.Now()
			log.Println("统计平均时间，用时："+endTime.Sub(startTime).String())
			log.Println(time.Unix(time.Now().Unix(), 0).String() + " 数据库查询结果条数:" +strconv.Itoa(len(result)))
			//			resultformatjson,errr := json.Marshal(result);_=errr
			//			log.Println(time.Unix(time.Now().Unix(), 0).String() + " 数据库查询结果:" + string(resultformatjson))
			runningorder.Setvalue(true,err,result)
		}
	})
	cron.Start()
}


/*
func Gettime_test()  {
	cron:=cron.New()
	cron.AddFunc("0 0 0 * * ?", func() {
		http.get
	})
	cron.Start()
}
*/

/*
func QueryRunningOrder(dba *dao.YcDB,runningorder *domain.RunningOrder)  {
	for   {
		result,err :=dba.QueryRunningOrder("")
		if err != nil {
			log.Println(time.Unix(time.Now().Unix(), 0).String() + " 数据库查询错误" + err.Error())
			runningorder.Setvalue(false,err,make([]map[string]interface{},0))
		}else {
			log.Println(time.Unix(time.Now().Unix(), 0).String() + " 数据库查询结果条数:" +strconv.Itoa(len(result)))
			resultformatjson,errr := json.Marshal(result);_=errr
			log.Println(time.Unix(time.Now().Unix(), 0).String() + " 数据库查询结果:" + string(resultformatjson))
			runningorder.Setvalue(true,err,result)
		}
		log.Println(time.Unix(time.Now().Unix(), 0).String() + " 休眠十五秒")
		time.Sleep(15*time.Second)
		log.Println(time.Unix(time.Now().Unix(), 0).String() + " 结束休眠")
	}
}
*/
